package com.jule.teenpatti.mail.dao;

import com.jule.core.database.DatabaseFactory;
import com.jule.teenpatti.mail.dao.bean.*;
import com.jule.teenpatti.mail.dao.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * Created by xujian on 2017/5/18 0018.
 */
@Slf4j
public class DBUtil {
    /**
     * 读取邮件配置列表
     *
     * @return
     */
    public static List<MailConfigModel> getMailConfigFromDb(long currentTimeMillis) {
        SqlSession sqlSession = DatabaseFactory.getInstance().getSqlSession();
        List<MailConfigModel> ret = null;
        try {
            ret = sqlSession.getMapper(MailConfigModelMapper.class).selectAllByExpireTime(currentTimeMillis);
            sqlSession.commit();
        } catch (Exception e) {
            log.error(e.getMessage());
            sqlSession.rollback();
        } finally {
            sqlSession.close();
        }
        return ret;
    }
    /**
     * 读取邮件配置列表
     *
     * @return
     */
    public static MailConfigModel getMailConfigFromDbById(int mailConfigId,long currentTimeMillis) {
        SqlSession sqlSession = DatabaseFactory.getInstance().getSqlSession();
        MailConfigModel ret = null;
        try {
            ret = sqlSession.getMapper(MailConfigModelMapper.class).selectByIdExpireTime(mailConfigId,currentTimeMillis);
            sqlSession.commit();
        } catch (Exception e) {
            log.error(e.getMessage());
            sqlSession.rollback();
        } finally {
            sqlSession.close();
        }
        return ret;
    }


    /**
     * 读取邮件参数配置列表
     *
     * @return
     */
    public static List<MailArgsModel> getMailArgsConfigFromDb(int mailConfigId) {
        SqlSession sqlSession = DatabaseFactory.getInstance().getSqlSession();
        List<MailArgsModel> ret = null;
        try {
            ret = sqlSession.getMapper(MailArgsModelMapper.class).selectByMailConfigId(mailConfigId);
            sqlSession.commit();
        } catch (Exception e) {
            log.error(e.getMessage());
            sqlSession.rollback();
        } finally {
            sqlSession.close();
        }
        return ret;
    }

    public static List<MailAttachmentConfigModel> getMailAttachmentFromDb(int mailConfigId) {
        SqlSession sqlSession = DatabaseFactory.getInstance().getSqlSession();
        List<MailAttachmentConfigModel> ret = null;
        try {
            ret = sqlSession.getMapper(MailAttachmentConfigModelMapper.class).selectByMailConfigId(mailConfigId);
            sqlSession.commit();
        } catch (Exception e) {
            log.error(e.getMessage());
            sqlSession.rollback();
        } finally {
            sqlSession.close();
        }
        return ret;
    }

    public static List<MailUserAwardLogModel> getMailUserAwardLogFromDb(int mailConfigId) {
        SqlSession sqlSession = DatabaseFactory.getInstance().getSqlSession();
        List<MailUserAwardLogModel> ret = null;
        try {
            ret = sqlSession.getMapper(MailUserAwardLogModelMapper.class).selectByMailConfigId(mailConfigId);
            sqlSession.commit();
        } catch (Exception e) {
            log.error(e.getMessage());
            sqlSession.rollback();
        } finally {
            sqlSession.close();
        }
        return ret;
    }

    public static List<MailUserModel> getMailUserFromDb(String userId, Long currentTimeMillis) {
        SqlSession sqlSession = DatabaseFactory.getInstance().getSqlSession();
        List<MailUserModel> ret = null;
        try {
            ret = sqlSession.getMapper(MailUserModelMapper.class).selectByUserId(userId, currentTimeMillis,0,100);
            sqlSession.commit();
        } catch (Exception e) {
            log.error(e.getMessage());
            sqlSession.rollback();
        } finally {
            sqlSession.close();
        }
        return ret;
    }

    /**
     * 大于0为插入成功
     *
     * @param record
     * @return
     */
    public static MailUserModel insertMailUser(String userId,MailUserModel record) {
        SqlSession sqlSession = DatabaseFactory.getInstance().getSqlSession();
        int ret = 0;
        try {
            ret = sqlSession.getMapper(MailUserModelMapper.class).insert(record);
            sqlSession.commit();
            if (ret > 0) {
                MailUserModel mailUserModel = sqlSession.getMapper(MailUserModelMapper.class).
                        selectByUserIdMailConfigId(userId,record.getMailConfigId());
                return mailUserModel;
            }
            sqlSession.commit();
        } catch (Exception e) {
            log.error(e.getMessage());
            sqlSession.rollback();
        } finally {
            sqlSession.close();
        }
        return null;
    }

    public static int insertMailUserAwardLog(MailUserAwardLogModel awardLogModel) {
        SqlSession sqlSession = DatabaseFactory.getInstance().getSqlSession();
        int ret = 0;
        try {
            ret = sqlSession.getMapper(MailUserAwardLogModelMapper.class).insert(awardLogModel);
            sqlSession.commit();
        } catch (Exception e) {
            log.error(e.getMessage());
            sqlSession.rollback();
        } finally {
            sqlSession.close();
        }
        return ret;
    }

    public static int updateMailUserByPrimaryKey(MailUserModel mailUserModel) {
        SqlSession sqlSession = DatabaseFactory.getInstance().getSqlSession();
        int ret = 0;
        try {
            ret = sqlSession.getMapper(MailUserModelMapper.class).updateByPrimaryKey(mailUserModel);
            sqlSession.commit();
        } catch (Exception e) {
            log.error(e.getMessage());
            sqlSession.rollback();
        } finally {
            sqlSession.close();
        }
        return ret;
    }

    public static int delMailUser(long id) {
        SqlSession sqlSession = DatabaseFactory.getInstance().getSqlSession();
        int ret = 0;
        try {
            ret = sqlSession.getMapper(MailUserModelMapper.class).deleteByPrimaryKey(id);
            sqlSession.commit();
        } catch (Exception e) {
            log.error(e.getMessage());
            sqlSession.rollback();
        } finally {
            sqlSession.close();
        }
        return ret;
    }
}
